﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;

/// <summary>
/// Summary description for AccessDatabase
/// </summary>
public class AccessDatabase
{
    private string strConn = WebConfigurationManager.ConnectionStrings["Connect"].ToString();
    //Ham ket noi database
    public SqlConnection getConnect()
    {
        return new SqlConnection(strConn);
    }

    //Ham tra ve 1 dataset
    public DataSet getDataset(string query)
    {
        SqlConnection conn = getConnect();
        SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
        DataSet dataset = new DataSet();
        adapter.Fill(dataset, "tenbang");
        return dataset;
    }

    //Ham tra ve 1 data table
    public DataTable getDataTable(string query)
    {
        SqlConnection conn = getConnect();
        SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
        DataTable table = new DataTable();
        adapter.Fill(table);
        return table;
    }

    //Ham thuc thi ExecuteNoneQuery
    public void getQuery(string query)
    {
        SqlConnection conn = getConnect();
        conn.Open();
        SqlCommand command = new SqlCommand(query, conn);
        command.ExecuteNonQuery();
        conn.Close();
        conn.Dispose();
    }

    //Ham thuc thi ExeCuteScalar
    public string getScalar(string query)
    {
        SqlConnection conn = getConnect();
        conn.Open();
        SqlCommand command = new SqlCommand(query, conn);
        string result = command.ExecuteScalar().ToString();
        conn.Close();
        conn.Dispose();
        return result;
    }

    //Ham thuc thi ExeCuteReader
    public SqlDataReader getReader(string query)
    {
        SqlConnection conn = getConnect();
        conn.Open();
        SqlCommand command = new SqlCommand(query, conn);
        SqlDataReader reader = command.ExecuteReader();
        return reader;
    }
}
